<template>
  <div>
    <a-modal
      centered
      :title="title"
      :width="1000"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
    >
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :span="10">
              <a-form-item label="行业">
                <a-input
                  placeholder="请输入行业"
                  v-model="queryParam.username"
                ></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="14">
              <span
                style="display: flex; overflow: hidden"
                class="table-page-search-submitButtons"
              >
                <a-button type="primary" @click="searchQuery" icon="search"
                  >查询</a-button
                >
                <a-button
                  type="primary"
                  @click="searchReset"
                  icon="reload"
                  style="margin-left: 8px"
                  >重置</a-button
                >
                <a-button
                  v-if="selectedRowKeys.length > 0"
                  @click="deleteBatch()"
                  type="primary"
                  icon="hdd"
                  style="border-right: 1px solid rgb(211, 209, 209)"
                  >批量解除</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- table区域-begin -->
      <div>
        <a-table
          size="small"
          bordered
          rowKey="id"
          :columns="columns1"
          :dataSource="dataSource1"
          :pagination="ipagination"
          :loading="loading"
          :scroll="{ y: 240 }"
          :rowSelection="{
            selectedRowKeys: selectedRowKeys,
            onSelectAll: onSelectAll,
            onSelect: onSelect,
            onChange: onSelectChange,
          }"
          @change="handleTableChange"
        >
          <span slot="action" slot-scope="text, record">
            <a @click="handleDelete(record)">解除绑定</a>
          </span>
        </a-table>
      </div>
      <!-- table区域-end -->
    </a-modal>
  </div>
</template>

<script>
import { filterObj } from "@/utils/util";
import { getAction } from "@/api/manage";
import { getBindingBoundary, relieveBindingBoundary } from "@/api/api";
export default {
  name: "SelectUserModal",
  data() {
    return {
      title: "已选行业",
      equipmentId: "",
      names: [],
      visible: false,
      placement: "right",
      description: "",
      // 查询条件
      queryParam: {},
      // 表头
      columns1: [
        {
          title: "行业",
          align: "left",
          dataIndex: "industryName",
        },
        {
          title: "排放源",
          align: "left",

          dataIndex: "boundaryName",
        },
        {
          title: "创建人",
          align: "left",

          dataIndex: "createBy",
        },
        {
          title: "创建时间",
          align: "left",

          dataIndex: "createTime",
        },
        {
          title: "操作",
          dataIndex: "action",
          scopedSlots: { customRender: "action" },
          align: "left",
        },
      ],
      columns2: [
        {
          title: "用户账号",
          align: "center",
          dataIndex: "username",
        },
        {
          title: "用户名称",
          align: "center",
          dataIndex: "realname",
        },
        {
          title: "操作",
          dataIndex: "action",
          align: "center",
          width: 100,
          scopedSlots: { customRender: "action" },
        },
      ],
      //数据集
      dataSource1: [],
      dataSource2: [],
      // 分页参数
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ["10", "20", "30"],
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条";
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
      },
      isorter: {
        column: "createTime",
        order: "desc",
      },
      loading: false,
      selectedRowKeys: [],
      selectedRows: [],
      ids: [],
    };
  },
  // created() {
  //   this.loadData();
  // },
  methods: {
    deleteBatch() {
      //   console.log(this.ids);
      var ids = this.ids.join(",");
      relieveBindingBoundary({ facilitiesId:this.equipmentId,boundaryIds: ids }).then((res) => {
        //   console.log(res);
        if (res.success) {
          this.loadData();
          this.$message.success(res.message);
        } else {
          this.$message.warning(res.message);
        }
      });
    },
    loadData() {
      getBindingBoundary({ facilitiesId: this.equipmentId }).then((res) => {
        //   console.log(res);
        this.dataSource1 = res.result;
      });
    },
    show(equipmentId) {
      this.visible = true;
      this.equipmentId = equipmentId;
      this.loadData();
    },
    handleDelete(boundary) {
      //   console.log(boundary)
      this.ids = [boundary.boundaryId];
      this.deleteBatch();
    },
    searchQuery() {
      this.loadData(1);
    },
    searchReset() {
      this.queryParam = {};
      this.loadData(1);
    },
    handleCancel() {
      this.visible = false;
    },
    handleOk() {
      this.dataSource2 = this.selectedRowKeys;
      //   console.log("data:" + this.dataSource2);
      this.$emit("selectFinished", this.dataSource2);
      this.visible = false;
    },
    add() {
      this.visible = true;
    },
    getQueryParams() {
      var param = Object.assign({}, this.queryParam, this.isorter);
      param.field = this.getQueryField();
      param.pageNo = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      return filterObj(param);
    },
    getQueryField() {
      //TODO 字段权限控制
    },

    onSelectAll(selected, selectedRows, changeRows) {
      var boundaryIdArr = [];
      selectedRows.forEach((item) => {
        boundaryIdArr.push(item.boundaryId);
      });
      this.ids = boundaryIdArr;
    },
    onSelect(record, selected) {
      if (this.ids.indexOf(record.boundaryId) > -1) {
        this.ids.splice(this.ids.indexOf(record.boundaryId), 1);
      } else {
        this.ids.push(record.boundaryId);
      }
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectedRows;
    },
    onClearSelected() {
      this.selectedRowKeys = [];
      this.selectionRows = [];
    },
 
    handleTableChange(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      //   console.log(sorter);
      //TODO 筛选
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field;
        this.isorter.order = "ascend" == sorter.order ? "asc" : "desc";
      }
      this.ipagination = pagination;
      this.loadData();
    },
  },
};
</script>
<style lang="scss" scoped>
.ant-card-body .table-operator {
  margin-bottom: 18px;
}

.ant-table-tbody .ant-table-row td {
  padding-top: 15px;
  padding-bottom: 15px;
}

.anty-row-operator button {
  margin: 0 5px;
}

.ant-btn-danger {
  background-color: #ffffff;
}

.ant-modal-cust-warp {
  height: 100%;
}

.ant-modal-cust-warp .ant-modal-body {
  height: calc(100% - 110px) !important;
  overflow-y: auto;
}

.ant-modal-cust-warp .ant-modal-content {
  height: 90% !important;
  overflow-y: hidden;
}
</style>